const { COUPON_DATE_TYPE, COUPON_OFFER_TYPE, COUPON_SCOPE_TYPE, COUPON_GET_TYPE } = require('../constants/market');
const { formatDate } = require('../common/utils');

const DATE_FORMAT = 'YYYY.MM.DD';

const dealAvalibleCouponItem = (item) => {
    const { offer_type, name, id, price, quota, use_period_type, use_begined,
        use_ended, available_after_got_days, days, qualify_types } = item.self;
    const isDiscount = offer_type == COUPON_OFFER_TYPE.discount; // 是否是折扣
    let valueTip = '';
    switch(offer_type) {
        case COUPON_OFFER_TYPE.discount:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.limit:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.cut:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.quotaCut:
            valueTip = quota > 0 ? `满${quota / 100}减${price / 100}` : '无门槛';
            break;
    }
    let validString = '';
    switch(use_period_type) {
        // 固定日期
        case COUPON_DATE_TYPE.fixed:
            validString = `${formatDate(use_begined, DATE_FORMAT)} - ${formatDate(use_ended, DATE_FORMAT)}`;
            break;
        // 领取后生效
        case COUPON_DATE_TYPE.after:
            const _arr = ['当日', '次日'];
            const len = available_after_got_days;
            validString = len > 1 ? `领取后${len}日生效` : `${_arr[len]}生效`;
            validString += `，${days}天有效`;
            break;
    }
    let isNeedMobile = false;
    // 判断领取条件
    if (qualify_types && qualify_types.indexOf(COUPON_GET_TYPE.mobile) > -1) {
        isNeedMobile = true;
    }

    return {
        id, // id
        name, // 名字
        value: price, // 优惠金额或优惠折扣
        valueTip, // 优惠提示
        isDiscount, // 是否是折扣
        validString, // 有效期
        isNeedMobile, // 是否需要绑定手机号
    };
};

const dealAvalibleCouponList = (list) => {
    const result = [];
    list.forEach(item => {
        const { distributed_cnt, got_cnt } = item.stat;
        if (distributed_cnt > got_cnt) {
            result.push(dealAvalibleCouponItem(item));
        }
    });
    return result;
};

const dealCouponItem = (item) => {
    const { offer_type, name, id, price, quota, begined, ended, scope, item_confs } = item;
    const isDiscount = offer_type == COUPON_OFFER_TYPE.discount; // 是否是折扣
    // const isLimit = offer_type == COUPON_OFFER_TYPE.limit; // 是否是特价
    // const isCut = offer_type == COUPON_OFFER_TYPE.cut; // 是否是减价
    // const isQuotaCut = offer_type == COUPON_OFFER_TYPE.quotaCut; // 是否是满减
    let valueTip = '';
    switch(offer_type) {
        case COUPON_OFFER_TYPE.discount:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.limit:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.cut:
            valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
            break;
        case COUPON_OFFER_TYPE.quotaCut:
            valueTip = quota > 0 ? `满${quota / 100}减${price / 100}` : '无门槛';
            break;
    }
    let useProductString = '';
    switch(scope) {
        case COUPON_SCOPE_TYPE.none:
        case COUPON_SCOPE_TYPE.unlimited:
            useProductString = '全部商品';
            break;
        case COUPON_SCOPE_TYPE.excludeCategory:
            break;
        case COUPON_SCOPE_TYPE.includeCategoryExcludeItems:
            break;
        case COUPON_SCOPE_TYPE.includeItems:
            useProductString = item_confs.map(item => item.name).join('、');
            break;
        case COUPON_SCOPE_TYPE.excludeItems:
            useProductString = item_confs.map(item => item.name).join('、') + '除外';
            break;
    }

    return {
        id, // id
        name, // 名字
        value: price, // 优惠金额或优惠折扣
        valueTip, // 优惠提示
        isDiscount, // 是否是折扣
        useDateString: `${formatDate(begined, DATE_FORMAT)} 至 ${formatDate(ended, DATE_FORMAT)}`, // 可用时间
        useProductString, // 可用商品
    };
};

const dealCouponList = (list) => {
    console.log(list);
    const result = [];
    list.forEach(item => {
        result.push(dealCouponItem(item));
    });
    return result;
};

module.exports = {
    dealAvalibleCouponItem,
    dealAvalibleCouponList,
    dealCouponItem,
    dealCouponList,
};